package com.aiyige.page.my.buy.adapter;

import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.PlayProgressModel;
import com.aiyige.model.Subject;
import com.aiyige.model.moment.backup.MediaSummary;
import com.aiyige.model.moment.entity.MajorCourse;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.model.moment.entity.TrainingCourse;
import com.aiyige.model.resource.UserResource;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyBuyAdapter extends BaseQuickAdapter<UserResource, BaseViewHolder> {

    public String getSubject() {
        return subject == null ? "" : subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    String subject = "";

    private boolean isEdit = false;


    public MyBuyAdapter() {
        super(new LinkedList<UserResource>());
    }

    @Override
    protected void convert(BaseViewHolder helper, UserResource item) {
        if (getSubject().equals(Subject.video_course)) {
            ((MyHolderVideoCourse) helper).bindData(item);
        } else if (getSubject().equals(Subject.major_course)) {
            ((MyHolderMajorCourse) helper).bindData(item);
        } else if (getSubject().equals(Subject.training_course)) {
            ((MyHolderTrainingCourse) helper).bindData(item);
        }

    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = null;
        if (getSubject().equals(Subject.video_course)) {
            baseViewHolder = new MyHolderVideoCourse(getItemView(getLayoutId(), parent));
        } else if (getSubject().equals(Subject.major_course)) {
            baseViewHolder = new MyHolderMajorCourse(getItemView(getLayoutId(), parent));
        } else if (getSubject().equals(Subject.training_course)) {
            baseViewHolder = new MyHolderTrainingCourse(getItemView(getLayoutId(), parent));
        }
        return baseViewHolder;
    }

    private int getLayoutId() {
        if (getSubject().equals(Subject.video_course)) {
            return R.layout.item_my_buy_video_course;
        } else if (getSubject().equals(Subject.major_course)) {
            return R.layout.item_my_buy_major_course;
        }
        if (getSubject().equals(Subject.training_course)) {
            return R.layout.item_my_buy_traning_course;
        }
        return R.layout.item_my_buy_video_course;
    }


    public void onViewClicked() {

    }

    /**
     * 教学视频
     */
    public class MyHolderVideoCourse extends BaseViewHolder {


        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.tv_content_count)
        TextView tvContentCount;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_last_study_position)
        TextView tv_last_study_position;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.ll_containerLayout)
        LinearLayout rlContent;


        public MyHolderVideoCourse(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }


        public void bindData(final UserResource data) {
            final Moment moment = !ListUtil.isEmpty(data.getResourceBackup()) ? data.getResourceBackup().get(0) : null;
            if (moment != null) {
                MediaSummary mediaSummary = moment.getMediaSummary();
                if (mediaSummary != null) {
                    if (moment.getSubject().equals(Subject.video_course) || (moment.getSubject().equals(Subject.video))) {
                        tvContentCount.setVisibility(mediaSummary.getCount() > 0 ? View.VISIBLE : View.INVISIBLE);
                        tvContentCount.setText(mContext.getString(R.string.content_count_value, mediaSummary.getCount() + ""));
                        tvTime.setVisibility(View.VISIBLE);
                        tvTime.setText(TimeUtils.getDurationTime(mediaSummary.getDuration()));
                    }
                }
                Spanned result;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    result = Html.fromHtml(moment.getTitle(), Html.FROM_HTML_MODE_LEGACY);
                } else {
                    result = Html.fromHtml(moment.getTitle());
                }
                if (!ListUtil.isEmpty(moment.getCover())) {
                    GlideUtil.with(mContext).big().loadCover(moment.getCover().get(0)).into(ivCover);
                }
                tvTitle.setText(data.isSearch()?result:moment.getTitle());
                if (!ListUtil.isEmpty(data.getStudyProgressBackup())) {
                    PlayProgressModel playProgressModel = data.getStudyProgressBackup().get(0);// 当前需求只记录和显示最后一次播放的那一集
                    tv_last_study_position.setVisibility(View.VISIBLE);
                    tv_last_study_position.setText(mContext.getString(R.string.last_study_value, String.valueOf(playProgressModel.getStage()),
                            TimeUtils.getDurationTime(playProgressModel.getProgress())));
                } else {
                    tv_last_study_position.setVisibility(View.INVISIBLE);
                }
                rlContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Router.start(data.getRouter());
                    }
                });
            }

        }


    }

    /**
     * 专业课程
     */
    public class MyHolderMajorCourse extends BaseViewHolder {

        @BindView(R.id.iv_cover)
        ImageView ivCover;

        @BindView(R.id.tv_title)
        TextView tvTitle;

        @BindView(R.id.tv_city)
        TextView tvCity;

        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.ll_containerLayout)
        LinearLayout rlContent;

        @BindView(R.id.tv_start_study_date)
        TextView tvStartStudyDate;

        @BindView(R.id.tv_end_study_date)
        TextView tvEndStudyDate;


        public MyHolderMajorCourse(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }


        public void bindData(final UserResource data) {

            final Moment moment = !ListUtil.isEmpty(data.getResourceBackup()) ? data.getResourceBackup().get(0) : null;
            if (moment != null) {
                Spanned result;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    result = Html.fromHtml(moment.getTitle(), Html.FROM_HTML_MODE_LEGACY);
                } else {
                    result = Html.fromHtml(moment.getTitle());
                }
                if (!ListUtil.isEmpty(moment.getCover())) {
                    GlideUtil.with(mContext).big().loadCover(moment.getCover().get(0)).into(ivCover);
                }
                tvTitle.setText(result);
                MajorCourse majorCourse = JsonUtil.toObject(moment.getMoreBackup(), MajorCourse.class);
                if (majorCourse != null) {
                    tvTime.setText(majorCourse.getClassCount() + mContext.getString(R.string.class_count));
                    tvStartStudyDate.setText(mContext.getString(R.string.start_class_info_value, majorCourse.getStartClassTime()));
                    tvEndStudyDate.setText(mContext.getString(R.string.end_class_info_value, majorCourse.getEndClassTime()));
                    tvCity.setText(majorCourse.getCityName(data.getCityId()));
                }

                rlContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Router.start(data.getRouter());

                    }
                });
            }


        }
    }


    /**
     * 培训班
     */
    public class MyHolderTrainingCourse extends BaseViewHolder {

        @BindView(R.id.iv_cover)
        ImageView ivCover;

        @BindView(R.id.tv_title)
        TextView tvTitle;

        @BindView(R.id.tv_city)
        TextView tvCity;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.ll_containerLayout)
        LinearLayout rlContent;

        @BindView(R.id.tv_start_study_date)
        TextView tvStartStudyDate;


        public MyHolderTrainingCourse(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }


        public void bindData(final UserResource data) {

            final Moment moment = !ListUtil.isEmpty(data.getResourceBackup()) ? data.getResourceBackup().get(0) : null;


            TrainingCourse trainingCourse = JsonUtil.toObject(moment.getMoreBackup(), TrainingCourse.class);
            if (trainingCourse != null) {
                tvTime.setText(trainingCourse.getClassCount() + mContext.getString(R.string.class_count));
                tvStartStudyDate.setText(mContext.getString(R.string.start_class_info_value, trainingCourse.getStartClassTime()));
                tvCity.setText(trainingCourse.getCityName());
            }

            Spanned result;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                result = Html.fromHtml(moment.getTitle(), Html.FROM_HTML_MODE_LEGACY);
            } else {
                result = Html.fromHtml(moment.getTitle());
            }
            if (!ListUtil.isEmpty(moment.getCover())) {
                GlideUtil.with(mContext).big().loadCover(moment.getCover().get(0)).into(ivCover);
            }


            tvTitle.setText(result);

            rlContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (moment.getSubject().endsWith(Subject.major_course)) {
                        Router.startAiyigeH5(moment.getRouter());
                    } else {
                        Router.start(moment.getRouter());
                    }
                }
            });

        }
    }


}
